package cst.com.example.yrx.myrecycle419.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.List;

import cst.com.example.yrx.myrecycle419.R;
import cst.com.example.yrx.myrecycle419.adapter.MyAdapter;
import cst.com.example.yrx.myrecycle419.bean.ArticleBean;
import cst.com.example.yrx.myrecycle419.iface.ArticleListener;
import cst.com.example.yrx.myrecycle419.model.ArticleModel;

/**
 * Created by Yrx on 2017/5/23.
 */
public class ArticleFragment extends Fragment {
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;
    private MyAdapter myAdapter;
    private List<ArticleBean> list=null;
    private View view=null;
    private Context context;
    //    private static int cur_page=1;
//    private static int lastVisibleItemPosition;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        view=inflater.inflate(R.layout.articlefragment,container,false);
        context=view.getContext();

        initRecyclerView();
        ArticleModel articleModel =new ArticleModel();
        articleModel.getResultList("article",2,"8soafn3tpaj0efuab5jvmpbhr4",articleListener);

        return view;
    }
    ArticleListener articleListener =new ArticleListener() {
        @Override
        public void onResponse(List<ArticleBean> listbean) {

            list=listbean;
            myAdapter.setList(list);

//          if(cur_page==1){
//              list=listbean;
//              myAdapter.setList(list);
//
//          }else{
//              list.removeAll(listbean);
//
//              list.addAll(0,listbean);
//              myAdapter.setList(list);
//          }

        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(context,"fail:"+msg,Toast.LENGTH_SHORT).show();
        }
    };
    private void initRecyclerView() {
        recyclerView=(RecyclerView)view.findViewById(R.id.rv_article);

        //创建默认的线性LayoutManager
        layoutManager =new LinearLayoutManager(context);
        //设置布局
        recyclerView.setLayoutManager(layoutManager);

        //设置item的高度
        recyclerView.setHasFixedSize(true);
        //创建适配器
//        list=getData();
        myAdapter =new MyAdapter(context);

        myAdapter.setList(list);

        recyclerView.setAdapter(myAdapter);
//        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
//            @Override
//            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
//
//                super.onScrollStateChanged(recyclerView, newState);
//                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItemPosition + 1 == list.size()) {
//                    cur_page+=1;
//
//                    new ArticleModel().getResultList("article",cur_page,"8soafn3tpaj0efuab5jvmpbhr4",articleListener);
//                }
//            }
//
//            @Override
//            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
//                super.onScrolled(recyclerView, dx, dy);
//                lastVisibleItemPosition=list.size()-1;
//            }
//        });
    }

}
